import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, User } from '@/api/user'

const state = {
  token: getToken(),
  userInfo: {}

}
const mutations = {
  // 保存token
  setToken(state, token) {
    state.token = token
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  },
  setUserInfo(state, user) {
    state.userInfo = user
  }

}
const actions = {
  // 登录
  async loginAsync(store, data) {
    // 调用接口 --- 后面再完成
    // 先假设token已经获取到了
    const token = await login(data)
    // console.log(token)
    store.commit('setToken', token)
  },
  async getUserInfo(store, data) {
    const res = await User()
    console.log(res)
    store.commit('setUserInfo', res)
  },
  logout(store) {
    store.commit('removeToken')
    store.commit('setUserInfo', {})
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
